package io.dshubs.archives.controller;


import io.dshubs.archives.dao.ArchiveDestroyMapper;
import io.dshubs.archives.entity.ArchiveAppraisal;
import io.dshubs.archives.entity.ArchiveDestroy;
import io.dshubs.archives.service.ArchiveAppraisalService;
import io.dshubs.archives.service.ArchiveDestroyService;
import io.dshubs.commons.log.annotation.LogOperation;
import io.dshubs.commons.tools.constant.Constant;
import io.dshubs.commons.tools.exception.RenException;
import io.dshubs.commons.tools.page.PageData;
import io.dshubs.commons.tools.utils.Result;
import io.dshubs.commons.tools.validator.ValidatorUtils;
import io.dshubs.commons.tools.validator.group.AddGroup;
import io.dshubs.commons.tools.validator.group.DefaultGroup;
import io.dshubs.commons.tools.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案销毁表 前端控制器
 * </p>
 *
 * @author tan
 * @since 2021-08-27
 */
@Api(tags = "档案销毁表")
@RestController
@RequestMapping("/destroy")
public class ArchiveDestroyController {

    @Autowired
    private ArchiveDestroyService archiveDestroyService;

    @Autowired
    private ArchiveDestroyMapper archiveDestroyMapper;


    @GetMapping("page")
    @ApiOperation("档案销毁:page")
    @LogOperation("档案销毁:/archive/destroy:/img/icon-档案销毁.png:page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = "destroyTitle", value = "题名", paramType = "query", dataType="String"),
            @ApiImplicitParam(name = "fileNumber", value = "档号", paramType = "query", dataType="String")
    })

    public Result<PageData<ArchiveDestroy>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        return new Result<PageData<ArchiveDestroy>>().ok(archiveDestroyService.page(params));
    }

    @GetMapping("{id}")
    @ApiOperation("详情")
    @LogOperation("档案销毁:get")
    public Result<ArchiveDestroy> get(@PathVariable("id") Long id){
        return new Result<ArchiveDestroy>().ok(archiveDestroyService.selectById(id));
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("档案销毁:save")
    public Result save(@RequestBody ArchiveDestroy archiveDestroy){
        //效验数据
        ValidatorUtils.validateEntity(archiveDestroy, AddGroup.class, DefaultGroup.class);

        archiveDestroyService.insert(archiveDestroy);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("档案销毁:update")
    public Result update(@RequestBody ArchiveDestroy archiveDestroy){
        //效验数据
        ValidatorUtils.validateEntity(archiveDestroy, UpdateGroup.class, DefaultGroup.class);

        archiveDestroyService.updateById(archiveDestroy);

        return new Result();
    }

    @DeleteMapping()
    @ApiOperation("删除")
    @LogOperation("档案销毁:delete")
    public Result delete(@RequestBody @NotNull(message = "批量删除id不能为空") List<Long> ids){
        archiveDestroyService.deleteBatchIds(ids);
        return new Result();
    }

    @PutMapping("/destroyed")
    @ApiOperation("修改")
    @LogOperation("档案销毁:update")
    public Result update(@RequestBody @NotNull(message = "id不能为空") List<String> ids){
        List<ArchiveDestroy> archiveDestroys = archiveDestroyMapper.selectBatchIds(ids);
        if(CollectionUtils.isNotEmpty(archiveDestroys)) {
            archiveDestroys.forEach(item->{
                item.setDestroyState("1");
                this.archiveDestroyService.updateById(item);
            });
        }else {
            throw new RenException("未选择销毁文件");
        }

        return new Result();
    }
}
